package cn.whpu.springboot.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@Controller
public class JdbcController {

    @Autowired
    JdbcTemplate jdbcTemplate;

    /**
     * 查询数据库，并将结果返回到网页
     * @return
     */
    @ResponseBody
    @GetMapping("/userList")
    public List<Map<String,Object>> userList(){
        String sql ="select * from user";
        List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql);
        return maps;
    }

    /**
     * 添加数据到user表
     * @return
     */
    @GetMapping("/addUser")
    public String addUser(){
        String sql ="insert into user(username,password) values (?,?)";
        jdbcTemplate.update(sql,"张三","123456");
        return "userList";
    }

    /**
     * 修改User中的数据
     * @return
     */
    @GetMapping("/updateUser")
    public String updateUser(){
        String sql = "update user set username=?,password=? where username=?";
        jdbcTemplate.update(sql,"李四","68888","张三");
        return "userList";
    }

    @GetMapping("/deleteUser")
    public String deleteUser(){
        String sql = "delete  from user where username=?";
        jdbcTemplate.update(sql,"李四");
        return "userList";
    }
}
